package com.wx.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wx.service.ShortURLService;

@Controller
@RequestMapping("/admin/t")
public class ShortURLController {
	@Resource
	private ShortURLService shortURLService;
	@RequestMapping(value = "/{key}")
	public void index(@PathVariable String key,HttpServletRequest req, HttpServletResponse res) throws Exception {
		String location = shortURLService.getShortURL(key);
		if(location!=null && location.startsWith("/")){
			int port = req.getServerPort();
			String basePath = req.getScheme()+"://"+req.getServerName();
			if(port!=80){
				basePath=basePath+":"+port;
			}
			basePath += req.getContextPath();
			res.sendRedirect(basePath+location);
		}else{
			res.sendRedirect(location);
		}
	}
}
